package org.csu.mypetstore.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.tomcat.util.json.JSONParser;
import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.domain.Product;
import org.csu.mypetstore.result.Result;
import org.csu.mypetstore.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.HtmlUtils;
import com.alibaba.fastjson.JSON;

import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

@Controller
public class ProductController {
    @Autowired
    private ProductService productService;

    @CrossOrigin
    @PostMapping(value = "api/dashboard")
    @ResponseBody
    //quest是请求
    public String common_Product(@RequestBody String quest) {
        //String[] array=  quest.split("\\.");
        //System.out.println(array[1]);
        JSONObject jsonObject = JSON.parseObject(quest);
        //请求获取销量前四的名字和数量
        if(jsonObject.getString("quest").equals("percentage_Product"))
        {

            //按照这个格式返回销量前四的名字和数量，比如——"pro1,90,pro2,80,pro3,70,pro4,60"
            return "pro1,90,pro2,80,pro3,70,pro4,60";
        }
        //请求今日订单量
         if(jsonObject.getString("quest").equals("todayOrderNumber"))
        {
            return "1234";
        }
         //请求未完成订单的数量
         if(jsonObject.getString("quest").equals("toFinishOrder")) {
             return "4321";
         }
         //请求缺货产品的数量
        if(jsonObject.getString("quest").equals("supplyShort")){
            return "666";
        }
        //发送toDoList
        if(jsonObject.getString("quest").equals("todo")){
            System.out.println("todo");
            //返回的todolist为title,格式例如"title1,title2"
            return "aaa,bbb";
        }
        //创建新的todoList
        if(jsonObject.getString("quest").equals("newList"))
        {
            //quest为新增的内容
            return "";
        }
        //删除todoList
        if(jsonObject.getString("quest").equals("deltodo"))
        {
            //quest为删除的内容
            return "";
        }
        else {
            System.out.println("error");
            return "";
        }

    }
    static String lastName;

    @CrossOrigin
    @PostMapping(value = "api/editProduct")
    @ResponseBody
    public String editProduct(@RequestBody String quest) {
        //String[] array = quest.split("\\.");
        JSONObject jsonObject = JSON.parseObject(quest);
        System.out.println(jsonObject);


        //获取商品信息
        if(jsonObject.getString("quest").equals("getProduct"))
        {
            List<Product> productList = productService.getProductList();
            String ret = "";
            for (int i=0; i<productList.size(); i++){
                Product product = productList.get(i);
                ret += product.getItemId() + ",";
                ret += product.getQuantity() + ",";
                ret += product.getListPrice() + ",";
                ret += product.getProductName() + ",";
                ret += product.getSales() + ",";
                ret += product.getCategory() + ",";
                ret += product.getAttribute1();
                if (i != productList.size()-1) ret += ",";
            }
//            System.out.println(ret);
            return ret;
            //按这个格式返回商品，"姓名，库存，价格，产地，优惠，近一个月销售量，类别，描述"
//            return "金毛,50,230,福建,1,狗,老好看了," +
//                    "小黑,20,690,山东,0,猫,可爱死了";
        }
        //删除商品
        if(jsonObject.getString("quest").equals("del"))
        {
            String itemid = jsonObject.getString("name");
            productService.deleteProduct(itemid);
            productService.deleteInventory(itemid);
            //前端会将商品名传过来
            return "ok";
        }
        //搜索商品
        if(jsonObject.getString("quest").equals("searchbyProductname"))
        {
 //           System.out.println("请求"+quest);
 //           System.out.println(jsonObject.getString("quest"));
            //前端会将商品名传过来
            String keyword = jsonObject.getString("state");
            List<Product> productList = productService.searchProductList(keyword);
            String ret = "";
            for (int i=0; i<productList.size(); i++){
                Product product = productList.get(i);
                ret += product.getItemId() + ",";
                ret += product.getQuantity() + ",";
                ret += product.getListPrice() + ",";
                ret += product.getProductName() + ",";
                ret += product.getSales() + ",";
                ret += product.getCategory() + ",";
                ret += product.getAttribute1();
                if (i != productList.size()-1) ret += ",";
            }

            return ret;
            //按这个格式返回商品，"姓名，库存，价格，产地，近一个月销售量，类别，描述"
            //return "小金毛,50,230,福建,1,狗,老好看了,小小黑,20,690,山东,没有折扣,0,猫,可爱死了";
        }
        //新增商品
        if(jsonObject.getString("quest").equals("newproduct"))
        {
            if (productService.getProductByItemid(jsonObject.getString("name")) != null){
                return "newFail";
            }else {
                Product product = new Product();
                product.setItemId(jsonObject.getString("name"));
                product.setAttribute1(jsonObject.getString("des"));
                product.setListPrice(new BigDecimal(jsonObject.getString("money")));
                product.setQuantity(Integer.parseInt(jsonObject.getString("number")));
                product.setSales(0);
                //前端会将商品修改的信息传过来
                String productId = productService.getProductidByname(jsonObject.getString("address"));
                product.setProductId(productId);
                productService.insertProduct(product);
                productService.insertInventory(product);
                //修改成功之后返回成功信息
                //System.out.println(quest);
                return "newSuccess";
            }
        }

        if (jsonObject.getString("quest").equals("edit")){
            lastName = jsonObject.getString("name");
            return "";
        }

        if (jsonObject.getString("quest").equals("editProInfo")){
            if (!jsonObject.getString("name").equals(lastName)){
                if (productService.getProductByItemid(jsonObject.getString("name")) != null){
                    System.out.println("no");
                    return "editFail";
                }else {
                    Product product = new Product();
                    product.setItemId(jsonObject.getString("name"));
                    product.setAttribute1(jsonObject.getString("des"));
                    product.setListPrice(new BigDecimal(jsonObject.getString("money")));
                    product.setQuantity(Integer.parseInt(jsonObject.getString("number")));
                    product.setLastName(lastName);
                    product.setSales(0);
                    //前端会将商品修改的信息传过来
                    String productId = productService.getProductidByname(jsonObject.getString("address"));
                    product.setProductId(productId);
                    productService.updateProduct(product);
                    productService.updateInventory(product);

                    return "editSuccess";
                }
            } else {
                System.out.println("yes");
                Product product = new Product();
                product.setItemId(jsonObject.getString("name"));
                product.setAttribute1(jsonObject.getString("des"));
                product.setListPrice(new BigDecimal(jsonObject.getString("money")));
                product.setQuantity(Integer.parseInt(jsonObject.getString("number")));
                product.setLastName(lastName);
                product.setSales(0);
                //前端会将商品修改的信息传过来
                String productId = productService.getProductidByname(jsonObject.getString("address"));
                product.setProductId(productId);
                productService.updateProduct(product);
                productService.updateInventory(product);

                return "editSuccess";
            }
        }

        else {
            System.out.println("error");
            return "";
        }
    }

}
